package com.trace.wells.charactertracker;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class FileHelper {

	private static final String SPELLFILE_A = "SpellsA.txt";
	private static final String SPELLFILE_C = "SpellsC.txt";
	private static final String SPELLFILE_D = "SpellsD.txt";
	private static final String SPELLFILE_F = "SpellsF.txt";
	private static final String SPELLFILE_H = "SpellsH.txt";
	private static final String SPELLFILE_M = "SpellsM.txt";
	private static final String SPELLFILE_P = "SpellsP.txt";
	private static final String SPELLFILE_S = "SpellsS.txt";
	private static final String SPELLFILE_T = "SpellsT.txt";

	private static final String SPELLFILE_MIN_A = "SpellsAMin.txt";
	private static final String SPELLFILE_MIN_C = "SpellsCMin.txt";
	private static final String SPELLFILE_MIN_D = "SpellsDMin.txt";
	private static final String SPELLFILE_MIN_F = "SpellsFMin.txt";
	private static final String SPELLFILE_MIN_H = "SpellsHMin.txt";
	private static final String SPELLFILE_MIN_M = "SpellsMMin.txt";
	private static final String SPELLFILE_MIN_P = "SpellsPMin.txt";
	private static final String SPELLFILE_MIN_S = "SpellsSMin.txt";
	private static final String SPELLFILE_MIN_T = "SpellsTMin.txt";

	private static final String NOFILE = "NOFILE";

	ArrayList<Character> characterList = new ArrayList<Character>();

	public FileHelper() {
	}

	public FileHelper(ArrayList<Character> charList) {
		characterList = charList;
	}

	public ArrayList<Character> returnCharactersFromFile(String fileName) {
		characterList.clear();
		readCharacters(fileName);

		return characterList;
	}

	public ArrayList<Character> readCharacters(String filename) {

		ArrayList<Character> characterList = new ArrayList<Character>();
		ObjectInputStream inputStream = null;

		try {

			//Construct the ObjectInputStream object
			inputStream = new ObjectInputStream(new FileInputStream(filename));

			Character obj = null;

			while ((obj = (Character) inputStream.readObject()) != null) {

				if (obj instanceof Character) {
					characterList.add(obj);
					Log.d("CHAR", "READ IN: " + obj.info.getName());
				}

			}

		} catch (EOFException ex) { //This exception will be caught when EOF is reached
			Log.d("CHAR", "Reached EOF");
		} catch (ClassNotFoundException ex) {
			Log.d("CHAR", "ClassNotFoundException");
		} catch (FileNotFoundException ex) {
			Log.d("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.d("CHAR", "IOException");
		} finally {
			//Close the ObjectInputStream
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		this.characterList = characterList;
		return characterList;
	}

	public void writeCharacters(String filename, ArrayList<Character> charArray) {

		ObjectOutputStream outputStream = null;

		try {

			//Construct the LineNumberReader object
			outputStream = new ObjectOutputStream(new FileOutputStream(filename));

			for (Character ch : charArray) {
				outputStream.writeObject(ch);
				Log.d("CHAR", "WROTE OUT: " + ch.info.getName());
			}

		} catch (FileNotFoundException ex) {
			Log.d("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.d("CHAR", "IOException");
		} finally {
			//Close the ObjectOutputStream
			try {
				if (outputStream != null) {
					outputStream.flush();
					outputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	public Spell getSpellInformationBasedOnName(String spellName, Context context, int charClass) {
		Spell returnSpell = null;
		try {
			InputStream is = null;
			String fileName = getFileNameFromSpellName(spellName);
			AssetManager assets = context.getAssets();
			is = assets.open(fileName);
			returnSpell = ConstantSpellHelper.getSpellData(spellName, is, charClass);

		} catch (EOFException ex) { //This exception will be caught when EOF is reached
			Log.d("CHAR", "Reached EOF");
		} catch (FileNotFoundException ex) {
			Log.e("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.e("CHAR", "IOException");
		} finally {

		}
		return returnSpell;

	}
	
	public SpellListItem getMinSpellInformationBasedOnName(String spellName, PrepareSpells prepareSpells, int charClass) {
		SpellListItem returnSpell = null;
		try {
			InputStream is = null;
			String fileName = getFileNameFromSpellName(spellName);
			AssetManager assets = prepareSpells.getAssets();
			is = assets.open(fileName);
			returnSpell = ConstantSpellHelper.getMinimumSpellData(spellName, is, charClass);

		} catch (EOFException ex) { //This exception will be caught when EOF is reached
			Log.d("CHAR", "Reached EOF");
		} catch (FileNotFoundException ex) {
			Log.e("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.e("CHAR", "IOException");
		} finally {

		}
		return returnSpell;

	}

	public static String getFileNameFromSpellName(String spellName) {
		String retStr;
		String beginningCharacter = spellName.substring(0, 1).toLowerCase();
		if (beginningCharacter.compareTo("a") >= 0 && beginningCharacter.compareTo("b") <= 0) {
			retStr = SPELLFILE_A;
		} else if (beginningCharacter.compareTo("c") >= 0 && beginningCharacter.compareTo("c") <= 0) {
			retStr = SPELLFILE_C;
		} else if (beginningCharacter.compareTo("d") >= 0 && beginningCharacter.compareTo("e") <= 0) {
			retStr = SPELLFILE_D;
		} else if (beginningCharacter.compareTo("f") >= 0 && beginningCharacter.compareTo("g") <= 0) {
			retStr = SPELLFILE_F;
		} else if (beginningCharacter.compareTo("h") >= 0 && beginningCharacter.compareTo("l") <= 0) {
			retStr = SPELLFILE_H;
		} else if (beginningCharacter.compareTo("m") >= 0 && beginningCharacter.compareTo("o") <= 0) {
			retStr = SPELLFILE_M;
		} else if (beginningCharacter.compareTo("p") >= 0 && beginningCharacter.compareTo("r") <= 0) {
			retStr = SPELLFILE_P;
		} else if (beginningCharacter.compareTo("s") >= 0 && beginningCharacter.compareTo("s") <= 0) {
			retStr = SPELLFILE_S;
		} else if (beginningCharacter.compareTo("t") >= 0 && beginningCharacter.compareTo("z") <= 0) {
			retStr = SPELLFILE_T;
		} else {
			retStr = NOFILE;
		}
		return retStr;
	}

	public static String getMinimumFileNameFromSpellName(String spellName) {
		String retStr;
		String beginningCharacter = spellName.substring(0, 1).toLowerCase();
		if (beginningCharacter.compareTo("a") >= 0 && beginningCharacter.compareTo("b") <= 0) {
			retStr = SPELLFILE_MIN_A;
		} else if (beginningCharacter.compareTo("c") >= 0 && beginningCharacter.compareTo("c") <= 0) {
			retStr = SPELLFILE_MIN_C;
		} else if (beginningCharacter.compareTo("d") >= 0 && beginningCharacter.compareTo("e") <= 0) {
			retStr = SPELLFILE_MIN_D;
		} else if (beginningCharacter.compareTo("f") >= 0 && beginningCharacter.compareTo("g") <= 0) {
			retStr = SPELLFILE_MIN_F;
		} else if (beginningCharacter.compareTo("h") >= 0 && beginningCharacter.compareTo("l") <= 0) {
			retStr = SPELLFILE_MIN_H;
		} else if (beginningCharacter.compareTo("m") >= 0 && beginningCharacter.compareTo("o") <= 0) {
			retStr = SPELLFILE_MIN_M;
		} else if (beginningCharacter.compareTo("p") >= 0 && beginningCharacter.compareTo("r") <= 0) {
			retStr = SPELLFILE_MIN_P;
		} else if (beginningCharacter.compareTo("s") >= 0 && beginningCharacter.compareTo("s") <= 0) {
			retStr = SPELLFILE_MIN_S;
		} else if (beginningCharacter.compareTo("t") >= 0 && beginningCharacter.compareTo("z") <= 0) {
			retStr = SPELLFILE_MIN_T;
		} else {
			retStr = NOFILE;
		}
		return retStr;
	}

}
